<?php

include('../../include/functions.inc.php');
include('../../include/inputbuilder.inc.php');

if (!isEmpty(getParam('operation'))) {
    switch (getParam('operation')) {
        case 's':
            save($db);
            break;
    }
}

function save($db) {
    $lDel = json_decode(stripslashes(getParam("jsondel")), TRUE);
    foreach ($lDel as $d) {
        $db->query_delete('cllg_courses_pensum', 'FK_PENSUM=' . sqlValue(getParam('CODE'), 'int') .
                ' AND FK_COURSE=' . sqlValue($d['CODIGO'], 'int'));
    }
    $lc = json_decode(stripslashes(getParam("json")), TRUE);
    foreach ($lc as $c) {
        if ($c["NUEVO"] == "TRUE") {
            $db->query_insert('cllg_courses_pensum', array(
                'FK_PENSUM' => sqlValue(getParam('CODE'), 'int'),
                'FK_COURSE' => sqlValue($c["CODIGO"], 'int'),
                'CYCLE' => sqlValue($c["CICLO"], 'int')));
        }
    }
    echo 'UPDATED';
}

?>